<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<jsp:include page="../common/import.jsp" flush="true"></jsp:include>
<body>
<jsp:include page="../common/header.jsp" flush="true"></jsp:include>

<div class="main p20">
	<div class="container clear">
        <ul class="left-nav fl pt20 w200">
            <li><a class="ahover" href="${base}/agent/agent/agentList.xhtml"><span class="iconfont">&#xf014b;</span>代理商</a></li>
            <li><a href="${base}/agent/sales/salesList.xhtml"><span class="iconfont">&#xf014d;</span>业务员</a></li>
            <li><a href="${base}/agent/merchant/merchantList.xhtml"><span class="iconfont">&#xf014d;</span>商户</a></li>
        </ul>
        
        <div class="main-right fr">
        	<div class="b m20 b-radius">
            	<div class="clear bgf5 bb">
                	<h2 class="fl h30 lg30 p5 pl10">编辑代理商</h2>
                    <a href="javascript:"  class="fr h20 p5 m5 w50 bg1eb9ef cfff b-radius tc"  onclick="back()">返回</a>
                    <!-- onclick="history.go(-1)" -->
                   
                </div>
                
                <input id="parentId" value="${agent.parentId}" hidden="true">
                <div class="p10 mt20">
                	<p class="bb c1eb9ef">基本信息</p>
                    <input hidden="true" id="agentId" value="${agent.id}">
                    <div class="clear mt20">
                    	<span class="fl w200 tr fb h20 lg20 p5">代理商名称：</span>
                        <input class="fl w400 h20 p5 b-radius" name="name" type="text" value="${agent.name }">
                    </div>
                    <c:if test="${agent.parentId=='0'}">
                    <div class="h30 lg30 clear mt20">
                        <span class="fl w200 tr fb h20 lg20 p5">级别：</span>
                        <c:if test="${agent.level=='1'}">
                        <input id="general_agent" type="radio" name="agent_level" value="1" checked="checked"><label for="general_agent" class="mr30">总代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="2"><label for="core_agent" class="mr30">核心代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="3"><label for="core_agent" class="mr30">普通代理</label>
                        </c:if>
                        <c:if test="${agent.level=='2'}">
                        <input id="general_agent" type="radio" name="agent_level" value="1"><label for="general_agent" class="mr30">总代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="2" checked="checked"><label for="core_agent" class="mr30">核心代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="3"><label for="core_agent" class="mr30">普通代理</label>
                        </c:if>
                        <c:if test="${agent.level=='3'}">
                        <input id="general_agent" type="radio" name="agent_level" value="1" checked="checked"><label for="general_agent" class="mr30">总代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="2"><label for="core_agent" class="mr30">核心代理</label>
                        <input id="core_agent" type="radio" name="agent_level" value="3" checked="checked"><label for="core_agent" class="mr30">普通代理</label>
                        </c:if>
                    </div>
                    </c:if>
                    
                    <div class="clear mt20">
                    	<span class="fl w200 tr fb h20 lg20 p5">授权区域：</span>
                    	 <select id="province" name="province" class="fl h30 lg30 b w200">
	                    	 	<c:choose>
	                    	 		<c:when test="${agent.province !=null}"><option>${agent.province}</option></c:when>
	                    			<c:otherwise><option>省份</option></c:otherwise>
	                   	 		</c:choose>
                            	<option>北京市</option>
            	            	<option>天津市</option>
            	            	<option>河北省</option>
            	            	<option>吉林省</option>
            	            	<option>上海市</option>
            	            	<option>山西省</option>
            	            	<option>内蒙古</option>
            	            	<option>辽宁省</option>
            	            	<option>黑龙江省</option>
            	            	<option>江苏省</option>
            	            	<option>浙江省</option>
            	            	<option>安徽省</option>
            	            	<option>福建省</option>
            	            	<option>江西省</option>
            	            	<option>山东省</option>
            	            	<option>河南省</option>
            	            	<option>湖北省</option>
            	            	<option>湖南省</option>
            	            	<option>广东省</option>
            	            	<option>广西</option>
            	            	<option>海南省</option>
            	            	<option>重庆市</option>
            	            	<option>西藏</option>
            	            	<option>宁夏</option>
            	            	<option>四川省</option>
            	            	<option>贵州省</option>
            	            	<option>云南省</option>
            	            	<option>陕西省</option>
            	            	<option>甘肃省</option>
            	            	<option>青海省</option>
            	            	<option>新疆</option>
                            
                        </select>
                        <select id="city" name="city" class="fl h30 lg30 b w200">
                    	 	 <c:choose>
	                    	 	<c:when test="${agent.city !=null}"><option>${agent.city}</option></c:when>
	                    	 </c:choose>
                           <!-- <option>城市</option> -->
                        </select>
                       <!--  <select class="fl h30 lg30 b w200">
                            <option>全部</option>
                            <option>河北省</option>
                            <option>上海市</option>
                        </select> -->
                    </div>
                    
                    <div class="clear mt20">
                    	<span class="fl w200 tr fb h20 lg20 p5">联系人：</span>
                        <input class="fl w400 h20 p5 b-radius" name="contactName" type="text" value="${agent.contactName}">
                    </div>
                    
                    <div class="clear mt20">
                    	<span class="fl w200 tr fb h20 lg20 p5">联系人手机号：</span>
                        <input class="fl w400 h20 p5 b-radius" name="contactPhone" type="text" value="${agent.contactPhone}">
                    </div>
                     <c:if test="${agent.parentId=='0'}">
                    <div class="h30 lg30 clear mt20">
                        <span class="fl w200 tr fb h20 lg20 p5">代理权：</span>
                        <c:if test="${agent.proxy=='1'}">
                        <input id="proxy_yes" type="radio" name="proxy" value="1" checked="checked"><label for="proxy_yes" class="mr30">有</label>
                        <input id="proxy_no" type="radio" name="proxy" value="0"><label for="proxy_no" class="mr30">无</label>
                        </c:if>
                         <c:if test="${agent.proxy=='0'}">
                        <input id="proxy_yes" type="radio" name="proxy" value="1" ><label for="proxy_yes" class="mr30">有</label>
                        <input id="proxy_no" type="radio" name="proxy" value="0" checked="checked"><label for="proxy_no" class="mr30">无</label>
                        </c:if>
                    </div>
                    </c:if>
                    <div class="tc p50">
                    	<button class="w100 bga0 h20 p10 b-radius" onclick="back()">取消</button>
                        <button class="w100 bgac1 cfff h20 p10 ml30 b-radius" onclick="submit()">保存</button>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
</div>

<jsp:include page="../common/footer.jsp" flush="true"></jsp:include>
<script type="text/javascript">
$(function(){
	var edit_city = '';
	$("#province").change(function () {
		var html = "<option value=''>城市</option>";
		$("#city").html(html);
		
		var prov = "prov";
		var value = $(this).val();
		var url = "http://www.wangpos.com/service/address/refreshCity";
		var data = {"key": prov,"value":value,"url":url};
        $.ajax({
			url: "${base}/agent/agent/authorZone.xhtml",
			type: "POST",
			data: data,
			dataType: "JSON",
			success: function (data) {
				var ison = data +";";
				if (data.status == 0) {
					hintError("数据错误");
					return;
				}
				var list = data.data;
				html = ''
				for (var i = 0; i < list.length; i++) {
					html += '<option value="' + list[i] + '">' + list[i] + '</option>';
				}
				// $("#prov").val(val);
				$("#city").html(html);
				if(edit_city != ''){
					$('#city').val(edit_city);
				}
				$('#city').change();
			},
			error: function (data) {
				alert('服务端报错' + data.status);
			}
		}); 
    });

});;
//返回列表页
function back(){
	window.location.href="${base}/agent/agent/agentList.xhtml";
} 
//提交数据
function submit(){
	var parentId = document.getElementById("parentId").value;
	var agentId = document.getElementById("agentId").value;
	var name = document.getElementsByName("name")[0].value;
	var level ='';
	var proxy = '';
	if(parentId=='0'){
		level = $('input:radio[name="agent_level"]:checked').val();
		if (level==''||level==null) {
			alert("级别不能为空！");
			return;
		}
		proxy = $('input:radio[name="proxy"]:checked').val();
		if (proxy==''||proxy==null) {
			alert("有无代理权不能为空！");
			return;
		}
	}
	var province = $("#province  option:selected").text();
	var city = $("#city  option:selected").text();
	/* if (city=='城市'||city==''||city==null) {
		alert("请选择城市");
		return;
	} */
	var authorZone = province+city;
	var contactName = document.getElementsByName("contactName")[0].value;
	var contactPhone = document.getElementsByName("contactPhone")[0].value;
	
	if(name==''||name==null){
		alert("代理商名称不能为空!");
		return;
	}
	
	if (province=='省份'||province==''||province==null) {
		alert("请选择省份");
		return;
	}
	if (authorZone==''||authorZone=='省份城市'||authorZone=='省份'||authorZone==null) {
		alert("授权区域不能为空！");
		return;
	}
	var mobile= contactPhone;
	if(mobile.length==0)
    {
       alert('请输入手机号码！');
       //document.form1.mobile.focus();
       return ;
    }    
    if(mobile.length!=11)
    {
        alert('请输入11位有效的手机号码！');
       // document.form1.mobile.focus();
        return ;
    }
    
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(mobile))
    {
        alert('请输入有效的手机号码！');
        //document.form1.mobile.focus();
        return ;
    }
    
	
	var url= "${base}/agent/agent/edit.xhtml";
	var data = {"agentId":agentId,"name":name,"level":level,"authorZone":authorZone,"province":province,"city":city,"contactName":contactName,"contactPhone":contactPhone,"proxy":proxy};
	
	$.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "json",
        success: function (data) {
        	//alert(data.info);
        	if (data.status==1) {
				alert(data.info);
				window.location.href="${base}/agent/agent/agentList.xhtml";
			}
        	if(data.status=='0'){
				alert(data.info);
			}
	        //location.reload(true);
        },
        error: function (data) {
            alert('服务端报错，' + data.status);
        }
    });
}
function validatemobile(mobile)
{
    if(mobile.length==0)
    {
       alert('请输入手机号码！');
       //document.form1.mobile.focus();
       return false;
    }    
    if(mobile.length!=11)
    {
        alert('请输入有效的手机号码！');
       // document.form1.mobile.focus();
        return false;
    }
    
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(mobile))
    {
        alert('请输入有效的手机号码！');
        //document.form1.mobile.focus();
        return false;
    }
}
</script>
</body>
</html>